package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskRimMatingBinding;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.entity.TaskRimMatingEntity;
import com.micangcaihang.micang.http.request.TaskRimMatingRequest;
import com.micangcaihang.micang.mvp.task.contract.TaskRimMatingContract;
import com.micangcaihang.micang.mvp.task.presenter.TaskRimMatingPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 周边配套
 *     version: 1.0
 * </pre>
 */

public class TaskRimMaingFragment extends BaseFragment<TaskRimMatingPresenter, FragmentTaskRimMatingBinding> implements TaskRimMatingContract.View {
    private String id;
    private int pickerType;
    private TaskRimMatingRequest mRequest;
    private TaskRimMatingRequest mOldRequest;
    private List<TaskRimMatingEntity.SupportIdBean> mBusiness;
    private List<TaskRimMatingEntity.SupportIdBean> mLife;
    private List<TaskRimMatingEntity.SupportIdBean> mOther;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static TaskRimMaingFragment newInstance(String id) {
        Bundle args = new Bundle();
        TaskRimMaingFragment fragment = new TaskRimMaingFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_rim_mating;
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new TaskRimMatingRequest();
        mOldRequest = new TaskRimMatingRequest();
        mBusiness = new ArrayList<>();
        mLife = new ArrayList<>();
        mOther = new ArrayList<>();
        mRequest.setId(id);
        mOldRequest.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
    }

    /**
     * 返回下拉框信息
     *
     * @param list
     * @param type
     */
    @Override
    public void returnPickerViewData(List<PickerEntity> list, String type) {
        Integer[] select;
        List<String> data = new ArrayList<>();
        for (PickerEntity entity : list) {
            data.add(entity.getTitle());
        }
        switch (pickerType) {
            case 1:
                if (mBusiness != null && mBusiness.size() > 0) {
                    select = new Integer[mBusiness.size()];
                    int k = 0;
                    for (int i = 0; i < mBusiness.size(); i++) {
                        TaskRimMatingEntity.SupportIdBean bean = mBusiness.get(i);
                        for (int j = 0; j < list.size(); j++) {
                            PickerEntity entity = list.get(j);
                            if (entity.getSign_id().equals(bean.getSupport_id())) {
                                select[k] = j;
                                k++;
                            }
                        }
                    }
                } else {
                    mBusiness = new ArrayList<>();
                    select = new Integer[]{};
                }
                PickerViewHelper.showMultiItem(_mActivity, type, data, select, (dialog, which, text) -> {
                    mBusiness.clear();
                    for (int i = 0; i < which.length; i++) {
                        TaskRimMatingEntity.SupportIdBean bean = new TaskRimMatingEntity.SupportIdBean();
                        bean.setSupport_id(list.get(which[i]).getSign_id());
                        bean.setSupport_name(list.get(which[i]).getTitle());
                        mBusiness.add(bean);
                    }
                    if (mBusiness != null && mBusiness.size() > 0) {
                        StringBuilder businessBuffer = new StringBuilder();
                        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mBusiness) {
                            businessBuffer.append(supportIdBean.getSupport_name()).append("、");
                        }
                        mViewBinding.tvBusiness.setText(businessBuffer.toString().substring(0, businessBuffer.toString().length() - 1));
                    }
                });
                break;
            case 2:
                if (mLife != null && mLife.size() > 0) {
                    select = new Integer[mLife.size()];
                    int k = 0;
                    for (int i = 0; i < mLife.size(); i++) {
                        TaskRimMatingEntity.SupportIdBean bean = mLife.get(i);
                        for (int j = 0; j < list.size(); j++) {
                            PickerEntity entity = list.get(j);
                            if (entity.getSign_id().equals(bean.getSupport_id())) {
                                select[k] = j;
                                k++;
                            }
                        }
                    }
                } else {
                    mLife = new ArrayList<>();
                    select = new Integer[]{};
                }
                PickerViewHelper.showMultiItem(_mActivity, type, data, select, (dialog, which, text) -> {
                    mLife.clear();
                    for (int i = 0; i < which.length; i++) {
                        TaskRimMatingEntity.SupportIdBean bean = new TaskRimMatingEntity.SupportIdBean();
                        bean.setSupport_id(list.get(which[i]).getSign_id());
                        bean.setSupport_name(list.get(which[i]).getTitle());
                        mLife.add(bean);
                    }
                    if (mLife != null && mLife.size() > 0) {
                        StringBuilder businessBuffer = new StringBuilder();
                        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mLife) {
                            businessBuffer.append(supportIdBean.getSupport_name()).append("、");
                        }
                        mViewBinding.tvLife.setText(businessBuffer.toString().substring(0, businessBuffer.toString().length() - 1));
                    }
                });
                break;
            case 3:
                if (mOther != null && mOther.size() > 0) {
                    select = new Integer[mOther.size()];
                    int k = 0;
                    for (int i = 0; i < mOther.size(); i++) {
                        TaskRimMatingEntity.SupportIdBean bean = mOther.get(i);
                        for (int j = 0; j < list.size(); j++) {
                            PickerEntity entity = list.get(j);
                            if (entity.getSign_id().equals(bean.getSupport_id())) {
                                select[k] = j;
                                k++;
                            }
                        }
                    }
                } else {
                    mOther = new ArrayList<>();
                    select = new Integer[]{};
                }
                PickerViewHelper.showMultiItem(_mActivity, type, data, select, (dialog, which, text) -> {
                    mOther.clear();
                    for (int i = 0; i < which.length; i++) {
                        TaskRimMatingEntity.SupportIdBean bean = new TaskRimMatingEntity.SupportIdBean();
                        bean.setSupport_id(list.get(which[i]).getSign_id());
                        bean.setSupport_name(list.get(which[i]).getTitle());
                        mOther.add(bean);
                    }
                    if (mOther != null && mOther.size() > 0) {
                        StringBuilder businessBuffer = new StringBuilder();
                        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mOther) {
                            businessBuffer.append(supportIdBean.getSupport_name()).append("、");
                        }
                        mViewBinding.tvOther.setText(businessBuffer.toString().substring(0, businessBuffer.toString().length() - 1));
                    }
                });
                break;
        }

    }

    /**
     * 返回周边配套信息
     *
     * @param entity
     */
    @Override
    public void returnRimMatingData(TaskRimMatingEntity entity) {
        isNetSuccess = true;
        if (StringUtils.isEmpty(entity.getCommunity_id())) {
            entity.setCommunity_id("0");
        }
        String[] excludsArray = new String[]{"choose_list", "id"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
            Properties.copyPropertiesExclude(entity, mOldRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mBusiness = entity.getBusiness_matching();
        mLife = entity.getLife_support();
        mOther = entity.getOther_support();
        //循环便利并设置请求数据
        List<TaskRimMatingRequest.SupportIdBean> business = new ArrayList<>();
        List<TaskRimMatingRequest.SupportIdBean> life = new ArrayList<>();
        List<TaskRimMatingRequest.SupportIdBean> other = new ArrayList<>();
        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mBusiness) {
            TaskRimMatingRequest.SupportIdBean request = new TaskRimMatingRequest.SupportIdBean();
            try {
                Properties.copyPropertiesExclude(supportIdBean, request, excludsArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            business.add(request);
        }
        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mLife) {
            TaskRimMatingRequest.SupportIdBean request = new TaskRimMatingRequest.SupportIdBean();
            try {
                Properties.copyPropertiesExclude(supportIdBean, request, excludsArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            life.add(request);
        }
        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mOther) {
            TaskRimMatingRequest.SupportIdBean request = new TaskRimMatingRequest.SupportIdBean();
            try {
                Properties.copyPropertiesExclude(supportIdBean, request, excludsArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            other.add(request);
        }
        mRequest.setBusiness_matching(business);
        mRequest.setLife_support(life);
        mRequest.setOther_support(other);
        mOldRequest.setBusiness_matching(business);
        mOldRequest.setLife_support(life);
        mOldRequest.setOther_support(other);


        StringBuilder businessBuffer = new StringBuilder();
        StringBuilder lifeBuffer = new StringBuilder();
        StringBuilder otherBuffer = new StringBuilder();
        if (mBusiness != null && mBusiness.size() > 0) {
            for (TaskRimMatingEntity.SupportIdBean supportIdBean : mBusiness) {
                businessBuffer.append(supportIdBean.getSupport_name()).append("、");
            }
            mViewBinding.tvBusiness.setText(businessBuffer.toString().substring(0, businessBuffer.toString().length() - 1));
        }
        if (mLife != null && mLife.size() > 0) {
            for (TaskRimMatingEntity.SupportIdBean supportIdBean : mLife) {
                lifeBuffer.append(supportIdBean.getSupport_name()).append("、");
            }
            mViewBinding.tvLife.setText(lifeBuffer.substring(0, lifeBuffer.toString().length() - 1));
        }
        if (mOther != null && mOther.size() > 0) {
            for (TaskRimMatingEntity.SupportIdBean supportIdBean : mOther) {
                otherBuffer.append(supportIdBean.getSupport_name()).append("、");
            }
            mViewBinding.tvOther.setText(otherBuffer.substring(0, otherBuffer.toString().length() - 1));
        }
    }

    public class ClickHandler {
        public void onBusiness(View view) {
            pickerType = 1;
            mPresenter.loadPickerViewData("business_matching", "商业配套");
        }

        public void onLife(View view) {
            pickerType = 2;
            mPresenter.loadPickerViewData("life_support", "生活配套");
        }

        public void onOther(View view) {
            pickerType = 3;
            mPresenter.loadPickerViewData("other_support", "其他配套");
        }

        public void onSave(View view) {
            saveData();
        }
    }

    /**
     * 开启保存数据请求
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = false;
        mPresenter.saveTuftMaingData(mRequest);
    }

    /**
     * 判断是否保存过数据 没有的话点击保存去保存
     *
     * @return 是否保存当前页面数据
     */
    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置请求数据
     */
    private void setSaveData() {
        List<TaskRimMatingRequest.SupportIdBean> business = new ArrayList<>();
        List<TaskRimMatingRequest.SupportIdBean> life = new ArrayList<>();
        List<TaskRimMatingRequest.SupportIdBean> other = new ArrayList<>();
        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mBusiness) {
            TaskRimMatingRequest.SupportIdBean request = new TaskRimMatingRequest.SupportIdBean();
            String[] excludsArray = new String[]{"choose_list", "id"};
            try {
                Properties.copyPropertiesExclude(supportIdBean, request, excludsArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            business.add(request);
        }
        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mLife) {
            TaskRimMatingRequest.SupportIdBean request = new TaskRimMatingRequest.SupportIdBean();
            String[] excludsArray = new String[]{"choose_list", "id"};
            try {
                Properties.copyPropertiesExclude(supportIdBean, request, excludsArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            life.add(request);
        }
        for (TaskRimMatingEntity.SupportIdBean supportIdBean : mOther) {
            TaskRimMatingRequest.SupportIdBean request = new TaskRimMatingRequest.SupportIdBean();
            String[] excludsArray = new String[]{"choose_list", "id"};
            try {
                Properties.copyPropertiesExclude(supportIdBean, request, excludsArray);
            } catch (Exception e) {
                e.printStackTrace();
            }
            other.add(request);
        }
        mRequest.setBusiness_matching(business);
        mRequest.setLife_support(life);
        mRequest.setOther_support(other);
    }
}
